<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>JS</title>
	<style>
		#box{
			width: 400px;
			height: 50px;
			border: 1px solid #999;
			padding: 30px;
			font-size: 25px;
		}
	</style>
</head>
<body>
	<h1>简易的计算器</h1>
	<hr>
	数1:<input type="text" id="num1"><br>
	数2:<input type="text" id="num2"><br>
	<button onclick="sum(1)">加</button>
	<button onclick="sum(2)">减</button>
	<button onclick="sum(3)">乘</button>
	<button onclick="sum(4)">除</button>
	<hr>
	<div id="box"></div>

	<script>
	//定义执行运算的函数
	function sum(m){
		//获取表单中的值
		var num1 = document.getElementById('num1').value;
		var num2 = document.getElementById('num2').value;
		console.log(num2);

		//判断 用户输入的是否为数字
		if (isNaN(num1)) {
			alert('数1 不是一个数字！');
			return;
		};

		if (isNaN(num2)) {
			alert('数2 不是一个数字！');
			return;
		};

		//执行数据转换
		num1 = Number(num1);
		num2 = Number(num2);

		switch(m){
			case 1:var result = num1 + num2;break;
			case 2:var result = num1 - num2;break;
			case 3:var result = num1 * num2;break;
			case 4:
				if(num2 == 0){
					alert('除数不能为0!!!');
					return;
				};
			var result = num1 / num2;break;
		}

		//输出结果到指定位置
		document.getElementById('box').innerHTML = result.toFixed(2);
		console.log(typeof(num1));

	}
	</script>
</body>
</html>